package com.gaofei;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@SpringBootTest
public class DemoProcess {

    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;
    /**
     * 部署流程
     */
    @Test
    public void deploy() {
        Deployment d = repositoryService.createDeployment()
                .name("请假流程")
                .addClasspathResource("process/qingjia.bpmn20.xml")
                .addClasspathResource("process/diagram.png")
                .deploy();
//
        System.out.println(d.getName());
        System.out.println(d.getId());
    }

    /**
     * 启动流程
     */
    @Test
    public void startProcess(){
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("qingjia");
        System.out.println("流程定义id:"+processInstance.getProcessDefinitionId()); //qingjia
        System.out.println("流程实例id:"+processInstance.getId()); //?
        System.out.println("当前活动id:"+processInstance.getActivityId());
    }


    /**
     * 根据负责人任务查询
     */
    @Test
    public void findTaskList(){
        String username = "zhangsan";

        List<Task> list = taskService.createTaskQuery()
                .processDefinitionKey("qingjia")
                .taskAssignee(username)
//                .taskCandidateUser("")   根据候选用户查询
                .list();

        System.out.println(list);

        list.forEach(task->{
            System.out.println(task.getProcessInstanceId());
            System.out.println(task.getId());
            System.out.println(task.getAssignee());
            System.out.println(task.getName());
        });
    }

    /**
     * 流程任务处理
     */

    @Test
    public void doTask(){
        //1.x先查询出待办任务
        Task qingjia = taskService.createTaskQuery().processDefinitionKey("qingjia")
                .taskAssignee("lisi")
                .singleResult();
        //2.处理待办任务,到下个节点
        Map<String, Object> variables = new HashMap<>();
        variables.put("appro", false);
        taskService.complete(qingjia.getId(),variables);
    }

    /**
     * 查询流程定义信息
     */
    @Test
    public void findProcessDefinitionInfo(){
        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();

        List<ProcessDefinition> qingjia = processDefinitionQuery.processDefinitionKey("qingjia")
                .orderByProcessDefinitionVersion()
                .desc()
                .list();
        System.out.println(qingjia);

        qingjia.forEach(pd->{
            System.out.println("流程定义 id="+pd.getId());
            System.out.println("流程定义 name="+pd.getName());
            System.out.println("流程定义 key="+pd.getKey());
            System.out.println("流程定义 Version="+pd.getVersion());
            System.out.println("流程部署ID ="+pd.getDeploymentId());
        });
    }

    /**
     * 删除流程
     */

    @Test
    public void del(){
        repositoryService.deleteDeployment("30664478-3ff5-11ee-bb64-f875a427a6cb",true);
    }
}
